<template>
  <van-button
    v-if="!value"
    class="follow-btn"
    type="info"
    color="#3296fa"
    round
    size="small"
    icon="plus"
    @click="onFollw"
    :loading="loding"
    >关注</van-button
  >
  <van-button
    v-else
    class="follow-btn"
    round
    size="small"
    @click="onFollw"
    :loading="loding"
    >已关注</van-button
  >
</template>

<script>
import { addFollow, deleteFollow } from '@/api/users'
export default {
  name: 'FollowUser',
  props: {
    value: {
      type: Boolean,
      require: true
    },
    userId: {
      type: [Number, String, Object],
      require: true
    }
  },
  data () {
    return {
      loding: false
    }
  },
  methods: {
    async onFollw () {
      this.loding = true
      console.log(this.value, this.userId)
      try {
        if (this.value) {
          await deleteFollow(this.userId)
        } else {
          await addFollow(this.userId)
        }
        this.$emit('input', !this.value)
        this.loding = false
      } catch (err) {
        let message = '操作失败，请重试'
        if (err.response && err.response.status === 400) {
          message = '你不能关注自己'
        }
        this.$toast(message)
      }
    }
  }
}
</script>

<style>
</style>
